package com.tszs.Controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import com.tszs.biz.UserReceivingAddressBiz;
import com.tszs.entity.UserReceivingAddress;
import com.tszs.entity.UserReceivingAddressExample;
import com.tszs.util.CallBack;
import com.tszs.util.Uuid;

/**
 * @类名：UserDetailsController
 * @类描述：个人收获地址
 * @author Administrator
 *
 */
@Controller
public class UserReceivingAddressController {
	/***
	 * @userDetailsBiz
	 */
	@Autowired
	private UserReceivingAddressBiz userReceivingAddressBiz;

	/**
	 * @方法名：addressAdd
	 * @方法描述：个人收获地址添加
	 * @param request
	 * @param response
	 */
	@RequestMapping("txr/addressAdd")
	public void addressAdd(HttpServletRequest request, HttpServletResponse response) {
		UserReceivingAddress record = new UserReceivingAddress();
		record.setuId(Uuid.getUUID());
		record.setuAddress(request.getParameter("uAddress"));
		record.setuPhone(request.getParameter("uPhone"));
		record.setuStatic("0");
		record.setuCreateTime(Uuid.getTime());
		record.setuOpenid(request.getSession().getAttribute("openid")+"");
		record.setuName(request.getParameter("uName"));
		record.setuDouble("0");
		int insertSelective = userReceivingAddressBiz.insertSelective(record);
		if (insertSelective > 0) {
			new CallBack().yes(response, "", "");
		} else {
			new CallBack().no(response, "", "");
		}
	}

	/**
	 * @方法名：addressselect
	 * @方法描述：个人收获地址查询
	 * @param request
	 * @param response
	 */
	@RequestMapping("txr/addressselect")
	public void addressselect(HttpServletRequest request, HttpServletResponse response) {
		UserReceivingAddressExample example = new UserReceivingAddressExample();
		com.tszs.entity.UserReceivingAddressExample.Criteria ca = example.createCriteria();
		ca.andUOpenidEqualTo(request.getSession().getAttribute("openid")+"");
		ca.andUStaticEqualTo("0");
		List<UserReceivingAddress> selectByExample = userReceivingAddressBiz.selectByExample(example);
		if (selectByExample.size() > 0) {
			new CallBack().yes(response, selectByExample, "");
		} else {
			new CallBack().no(response, selectByExample, "");
		}
	}

	/**
	 * @方法名：addressdelete
	 * @方法描述：个人收获地址删除
	 * @param request
	 * @param response
	 */
	@RequestMapping("txr/addressdelete")
	public void addressdelete(HttpServletRequest request, HttpServletResponse response) {
		UserReceivingAddressExample example = new UserReceivingAddressExample();
		com.tszs.entity.UserReceivingAddressExample.Criteria ca = example.createCriteria();
		ca.andUIdEqualTo(request.getParameter("UId"));
		int i = userReceivingAddressBiz.deleteByExample(example);
		if (i > 0) {
			new CallBack().yes(response, "", "");
		} else {
			new CallBack().no(response, "", "");
		}
	}

	/**
	 * @方法名：addressupdate
	 * @方法描述：地址设置默认
	 * @param request
	 * @param response
	 */
	@RequestMapping("txr/addressupdate")
	public void addressupdate(HttpServletRequest request, HttpServletResponse response) {
		String UId = request.getParameter("UId");
		UserReceivingAddress record = new UserReceivingAddress();
		record.setuDouble("1");
		UserReceivingAddressExample example = new UserReceivingAddressExample();
		com.tszs.entity.UserReceivingAddressExample.Criteria ca = example.createCriteria();
		ca.andUIdEqualTo(UId);
		int i = userReceivingAddressBiz.updateByExampleSelective(record, example);
		if (i > 0) {
			update(UId);
			new CallBack().yes(response, "", "");
		} else {
			new CallBack().no(response, "", "");
		}
	}

	public int update(String UId) {
		HttpServletRequest request = null;
		UserReceivingAddress record = new UserReceivingAddress();
		record.setuDouble("0");
		UserReceivingAddressExample example = new UserReceivingAddressExample();
		com.tszs.entity.UserReceivingAddressExample.Criteria ca2 = example.createCriteria();
		ca2.andUIdNotEqualTo(UId);
		int i2 = userReceivingAddressBiz.updateByExampleSelective(record, example);
		return i2;
	}

	/**
	 * @方法名：addressShop
	 * @方法描述：购物车默认地址
	 * @param request
	 * @param response
	 */
	@RequestMapping("txr/addressShop")
	public void addressShop(HttpServletRequest request, HttpServletResponse response) {
		UserReceivingAddressExample example = new UserReceivingAddressExample();
		com.tszs.entity.UserReceivingAddressExample.Criteria ca = example.createCriteria();
		ca.andUOpenidEqualTo(request.getSession().getAttribute("openid")+"");
		ca.andUDoubleEqualTo("1");//默认收货地址
		List<UserReceivingAddress> selectByExample = userReceivingAddressBiz.selectByExample(example);
		if (selectByExample.size() > 0) {
			new CallBack().yes(response, selectByExample, "");
		} else {
			UserReceivingAddressExample example2 = new UserReceivingAddressExample();
			com.tszs.entity.UserReceivingAddressExample.Criteria ca2 = example2.createCriteria();
			ca2.andUOpenidEqualTo(request.getSession().getAttribute("openid")+"");
			List<UserReceivingAddress> selectByExample2 = userReceivingAddressBiz.selectByExample(example2);
			if (selectByExample2.size() > 0) {
				new CallBack().yes(response, selectByExample2, "");
			} else {
				new CallBack().no(response, "", "");
			}
		}
	}
}
